package v1

import (
	"gin-vue-admin/global"
    "gin-vue-admin/model"
    "gin-vue-admin/model/request"
    "gin-vue-admin/model/response"
    "gin-vue-admin/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

// @Tags Tails
// @Summary 创建Tails
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Tails true "创建Tails"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /tails/createTails [post]
func CreateTails(c *gin.Context) {
	var tails model.Tails
	_ = c.ShouldBindJSON(&tails)
	if err := service.CreateTails(tails); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags Tails
// @Summary 删除Tails
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Tails true "删除Tails"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /tails/deleteTails [delete]
func DeleteTails(c *gin.Context) {
	var tails model.Tails
	_ = c.ShouldBindJSON(&tails)
	if err := service.DeleteTails(tails); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags Tails
// @Summary 批量删除Tails
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Tails"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /tails/deleteTailsByIds [delete]
func DeleteTailsByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteTailsByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags Tails
// @Summary 更新Tails
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Tails true "更新Tails"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /tails/updateTails [put]
func UpdateTails(c *gin.Context) {
	var tails model.Tails
	_ = c.ShouldBindJSON(&tails)
	if err := service.UpdateTails(tails); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags Tails
// @Summary 用id查询Tails
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Tails true "用id查询Tails"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /tails/findTails [get]
func FindTails(c *gin.Context) {
	var tails model.Tails
	_ = c.ShouldBindQuery(&tails)
	if err, retails := service.GetTails(tails.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retails": retails}, c)
	}
}

// @Tags Tails
// @Summary 分页获取Tails列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.TailsSearch true "分页获取Tails列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /tails/getTailsList [get]
func GetTailsList(c *gin.Context) {
	var pageInfo request.TailsSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetTailsInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

// 根据LineId查询数据列表
func GetTailsListByLineId(c *gin.Context) {
	var pageInfo request.TailsSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetTailsListByLineId(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// 根据LineId的最大创建时间查询最新记录
func GetTailsByMaxCreateTimeWhereLineId(c *gin.Context)  {
	var tails model.Tails
	_ = c.ShouldBindQuery(&tails)
	if err, newTails := service.GetTailsByMaxCreateTimeWhereLineId(tails.LinesId); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"newTails": newTails}, c)
	}
}